import axios from 'axios';

const API_URL = 'http://172.16.103.206:8081/api';

export interface Question {
    id: string;
    bankId: string;
    type: 'single' | 'multiple' | 'true-false' | 'essay';
    title: string;
    options: {
        text: string;
        isCorrect: boolean;
    }[];
    answer: string;
    analysis: string;
    bankName: string;
    createdAt: string;
}

export interface QuestionResponse {
    data: Question[];
    total: number;
}

export interface CreateQuestionData {
    bankId: string;
    type: Question['type'];
    title: string;
    options: Question['options'];
    answer: string;
    analysis: string;
}

export interface UpdateQuestionData extends Partial<CreateQuestionData> {}

// 获取题目列表
export const getQuestions = async (params?: {
    bankId?: string;
    type?: string;
    keyword?: string;
    sortBy?: string;
    page?: number;
    pageSize?: number;
}): Promise<QuestionResponse> => {
    const response = await axios.get(`${API_URL}/questions`, {
        params,
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 创建题目
export const createQuestion = async (data: CreateQuestionData): Promise<Question> => {
    const response = await axios.post(`${API_URL}/questions`, data, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 获取单个题目详情
export const getQuestionById = async (id: string): Promise<Question> => {
    const response = await axios.get(`${API_URL}/questions/${id}`, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 更新题目
export const updateQuestion = async (id: string, data: UpdateQuestionData): Promise<Question> => {
    const response = await axios.put(`${API_URL}/questions/${id}`, data, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 删除题目
export const deleteQuestion = async (id: string): Promise<void> => {
    await axios.delete(`${API_URL}/questions/${id}`, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
}; 